package com.wgke.bluetooth.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;

import com.wgke.bluetooth.R;


/**
 * 基类
 */
public abstract class BaseDialog extends Dialog {

    private Context mContext;
    OnCallback callback;
    OnCallbackArr callbackArr;

    public BaseDialog(@NonNull Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public BaseDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        this.mContext = context;
        init();
    }

    public void setCallback(OnCallback cb) {
        this.callback = cb;
    }
    public void setCallback(OnCallbackArr cb) {
        this.callbackArr = cb;
    }


    public abstract int layoutId();

    public abstract void initView();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layoutId());
        initView();
    }

    private void init() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(layoutId());
        Window window = getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            if (getAnimStyle() != 0) {
                window.setWindowAnimations(getAnimStyle());
            }
            window.setGravity(getGravity());
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.width = getDialogWidth(mContext);
            window.setAttributes(attributes);
        }
    }

    public int getGravity() {
        return Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
    }

    public int getAnimStyle() {
        return R.style.AlertDialog_Push;
    }

    /**
     * 获取对话框的宽
     */
    public int getDialogWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        if (wm != null) {
            wm.getDefaultDisplay().getMetrics(outMetrics);
        } else {
            return WindowManager.LayoutParams.MATCH_PARENT;
        }
        return Math.min(outMetrics.widthPixels, outMetrics.heightPixels);
    }

    public interface OnCallback {
        void chooseIndex(int index, int value);
    }

    public interface OnCallbackArr {
        void chooseResult(int[] arr);
    }
}
